/**
 * 用于将EditBox上的节点在该EditBox获取焦点的时候闪烁
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class InputIndicator extends cc.Component {

    @property(cc.Node)
    indicator: cc.Node = null;

    @property(cc.EditBox)
    editBox : cc.EditBox = null;

    @property(cc.Label)
    editLabel : cc.Label = null;

    indicatorOriX : number = null;

    onLoad () {
        this.indicator.active = false
        this.indicatorOriX = this.indicator.x
        this._initCallbacks()
    }

    _initCallbacks(){
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "InputIndicator";//这个是代码文件名
        clickEventHandler.handler = "showIndicator";
        // clickEventHandler.customEventData = "foobar";
        this.editBox.editingDidBegan.push(clickEventHandler)


        var clickEventHandler2 = new cc.Component.EventHandler();
        clickEventHandler2.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler2.component = "InputIndicator";//这个是代码文件名
        clickEventHandler2.handler = "stopIndicator";
        this.editBox.editingDidEnded.push(clickEventHandler2)


        var clickEventHandler3 = new cc.Component.EventHandler();
        clickEventHandler3.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler3.component = "InputIndicator";//这个是代码文件名
        clickEventHandler3.handler = "updateIndicatorPosition";
        this.editBox.textChanged.push(clickEventHandler3)
    }

    start () {

    }

    update (dt) {

    }

    showIndicator(){
        this.indicator.active = true
        this.updateIndicatorPosition()
        var fadeIn1 = cc.fadeIn(0.3)
        var fadeOut1 = cc.fadeOut(0.3)
        let seq1 = cc.sequence(fadeOut1,fadeIn1)
        let repeat1 = cc.repeatForever(seq1)
        this.indicator.runAction(repeat1)
    }

    stopIndicator(){
        this.indicator.active = false
        this.indicator.stopAllActions()
        this.indicator.opacity = 255;
    }

    updateIndicatorPosition(){
        this.editLabel._forceUpdateRenderData(true)
        this.indicator.x = this.indicatorOriX + this.editLabel.node.width + 4
        cc.log("indicator的x：",this.indicator.x)
    }
}
